package com.whmnx.my.ui.viewmodel;

import android.app.Application;
import android.app.Dialog;
import android.graphics.Bitmap;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import androidx.annotation.NonNull;

import com.whmnx.base.bean.BaseBean;
import com.whmnx.base.constants.Constants;
import com.whmnx.base.http.CallBack;
import com.whmnx.base.http.HttpClient;
import com.whmnx.base.utils.UItils;
import com.whmnx.my.R;
import com.whmnx.my.databinding.FragmentYaoBinding;
import com.whmnx.my.ui.fragment.MyYaoQingFragment;

import java.util.HashMap;
import java.util.Map;

import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.wechat.friends.Wechat;
import cn.sharesdk.wechat.moments.WechatMoments;
import me.goldze.mvvmhabit.binding.command.BindingAction;
import me.goldze.mvvmhabit.binding.command.BindingCommand;
import me.goldze.mvvmhabit.utils.SPUtils;

/**
 * author : huang
 * date   : 2020/9/29 0029 14:55
 * version: 1.0
 * @author Administrator
 */
public class YaoModel extends ToolbarViewModel{
    MyYaoQingFragment mFragment;
    FragmentYaoBinding mBinding;
    private Bitmap bitmap;

    public YaoModel(@NonNull Application application) {
        super(application);
    }

    public void initToolbar(MyYaoQingFragment myYaoQingFragment, FragmentYaoBinding binding) {
        this.mFragment = myYaoQingFragment;
        this.mBinding = binding;
        setTitleText("邀请好友");
        initView();
    }

    private void initView() {
        Map<String, Object> params = new HashMap<>();
        params.put("uId", SPUtils.getInstance().getString(Constants.USERID));
        HttpClient.get(mFragment.getActivity(), Constants.GETQRCODE, params,new CallBack<BaseBean>() {
            @Override
            public void onSuccess(BaseBean result) {  //1234567a
                bitmap = UItils.base64ToBitmap((String) result.getResult());
                mBinding.ivCode.setImageBitmap(bitmap);
            }
        });
    }



    public BindingCommand OnClickCommand = new BindingCommand(new BindingAction() {
        @Override
        public void call() {  //Balance
            UItils.saveToSystemGallery(mFragment.getActivity(),bitmap);
        }
    });

    public BindingCommand OnClickCommand2 = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            Dialog dialogHead = new Dialog(mFragment.getActivity(), R.style.ActionSheetDialogStyle);
            View view = LayoutInflater.from(mFragment.getActivity()).inflate(R.layout.dialog_share, null);

            view.findViewById(R.id.ll_wei).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                   cn.sharesdk.framework.Platform platform = ShareSDK.getPlatform(Wechat.NAME);
                    cn.sharesdk.framework.Platform.ShareParams shareParams = new  cn.sharesdk.framework.Platform.ShareParams();
                    shareParams.setTitle("【今网播】定制邀请函");
                shareParams.setText(SPUtils.getInstance().getString(Constants.NICKNAME) + "邀请您一起玩转短视频");
              //  shareParams.setImageUrl(videoBean.getWork_Image());
           //    shareParams.setImageUrl(videoBean.getWork_Image());
                shareParams.setUrl("http://w1.whmnx.com/h5/index.html?code=" + SPUtils.getInstance().getString(Constants.CODE));
                //分享网页
                shareParams.setShareType(4);
                platform.share(shareParams);
                dialogHead.dismiss();
                }
            });
            view.findViewById(R.id.ll_zhi).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                cn.sharesdk.framework.Platform platform = ShareSDK.getPlatform(WechatMoments.NAME);
                cn.sharesdk.framework.Platform.ShareParams shareParams = new  cn.sharesdk.framework.Platform.ShareParams();
                shareParams.setTitle("【今网播】定制邀请函");
                    shareParams.setText(SPUtils.getInstance().getString(Constants.NICKNAME) + "邀请您一起玩转短视频");
                //  shareParams.setImageUrl(videoBean.getWork_Image());
            //    shareParams.setImageUrl(videoBean.getWork_Image());
                    shareParams.setUrl("http://w1.whmnx.com/h5/index.html?code=" + SPUtils.getInstance().getString(Constants.CODE));
                //分享网页
                shareParams.setShareType(4);
                platform.share(shareParams);
                dialogHead.dismiss();
                }
            });

            dialogHead.setContentView(view);
            Window window = dialogHead.getWindow();
            if (window == null) {
                return;
            }
            //宽高最大
            window.setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT);
            //显示对话框//设置Dialog从窗体底部弹出
            dialogHead.setCanceledOnTouchOutside(true);
            dialogHead.show();
            window.setGravity(Gravity.BOTTOM);
            //获取窗体的属性
            WindowManager.LayoutParams lp = window.getAttributes();
            //设置Dialog距离底部的距离
            //将属性设置给窗体
            window.setAttributes(lp);
        }
    });
}
